package com.ruoyi.project.men.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.men.domain.MenFeedbackDO;
import com.ruoyi.project.men.domain.MenNeedInfoDO;
import com.ruoyi.project.men.service.MenFeedbackService;
import com.ruoyi.project.men.service.MenNeedInfoService;
import com.ruoyi.project.wx.domain.GoodsGoodsDO;
import com.ruoyi.project.wx.service.GoodsGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 意见反馈Controller
 *
 * @author long
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/men/menFeedback")
public class MenFeedbackController extends BaseController
{
	@Autowired
	private MenFeedbackService menFeedbackService;
	@Autowired
	private GoodsGoodsService goodsGoodsService;
	@Autowired
	private MenNeedInfoService menNeedInfoService;

	/**
	 * 查询意见反馈列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menFeedback:list')")
	@GetMapping("/list")
	public TableDataInfo list(MenFeedbackDO menFeedback)
	{
		try {
			startPage();
			Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menFeedback);
			stringObjectMap.put("delFlag","0");
			stringObjectMap.put("sort","ca_at");
			stringObjectMap.put("order","desc");
			if(menFeedback.getStatus()!=null){
				stringObjectMap.put("status",menFeedback.getStatus()+"");
			}
			if(menFeedback.getType()!=null){
				stringObjectMap.put("type",menFeedback.getType()+"");
			}

			List<MenFeedbackDO> menFeedbackList = menFeedbackService.likeList(stringObjectMap);

			//List<Map<String,Object>> list = new ArrayList<>();
			if(menFeedbackList.size()>0){
				for (MenFeedbackDO menFeedbackDO : menFeedbackList) {

						//Map<String,Object> describe = BeanUtils.describe(menFeedbackDO);
						if(menFeedbackDO.getType()==0){
							GoodsGoodsDO goodsGoodsDO = goodsGoodsService.get(menFeedbackDO.getBackId());
							menFeedbackDO.setBackName(goodsGoodsDO.getName());
							//describe.put("backName",goodsGoodsDO.getName());
							//menFeedbackDO.setBackId(goodsGoodsDO.getName());
						}else if(menFeedbackDO.getType()==1){
							MenNeedInfoDO menNeedInfoDO = menNeedInfoService.get(menFeedbackDO.getBackId());
							menFeedbackDO.setBackName(menNeedInfoDO.getName());
							//describe.put("backName",menNeedInfoDO.getName());
							//menFeedbackDO.setBackId(menNeedInfoDO.getName());
						}else{
							menFeedbackDO.setBackName("门户反馈");
							//describe.put("backName","门户反馈");
							//menFeedbackDO.setBackId("门户反馈");
						}
						//list.add(describe);
					}
				}
			return getDataTable(menFeedbackList);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}



	/**
	 * 导出意见反馈列表
	 */
	@PreAuthorize("@ss.hasPermi('men:menFeedback:export')")
	@Log(title = "意见反馈", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(MenFeedbackDO menFeedback)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menFeedback);
		List<MenFeedbackDO> list = menFeedbackService.list(stringObjectMap);
		ExcelUtil<MenFeedbackDO> util = new ExcelUtil<MenFeedbackDO>(MenFeedbackDO.class);
		return util.exportExcel(list, "menFeedback");
	}

	/**
	 * 获取意见反馈详细信息
	 */
	@PreAuthorize("@ss.hasPermi('men:menFeedback:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") String id)
	{
		return AjaxResult.success(menFeedbackService.get(id));
	}

	/**
	 * 新增意见反馈
	 */
	@PreAuthorize("@ss.hasPermi('men:menFeedback:add')")
	@Log(title = "意见反馈", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody MenFeedbackDO menFeedback)
	{
		menFeedback.setId(IdUtil.simpleUUID());
		menFeedback.setDelFlag(0);
		menFeedback.setCaAt(new Date());
		return toAjax(menFeedbackService.save(menFeedback));
	}

	/**
	 * 修改意见反馈
	 */
	@PreAuthorize("@ss.hasPermi('men:menFeedback:edit')")
	@Log(title = "意见反馈", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MenFeedbackDO menFeedback)
	{
		menFeedback.setUpAt(new Date());
		return toAjax(menFeedbackService.update(menFeedback));
	}

	/**
	 * 删除意见反馈
	 */
	@PreAuthorize("@ss.hasPermi('men:menFeedback:remove')")
	@Log(title = "意见反馈", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable String[] ids)
	{
		if(ids.length>0){
			for (String string : ids) {
				MenFeedbackDO menFeedback = menFeedbackService.get(string);
				menFeedback.setDelFlag(1);
				menFeedback.setUpAt(new Date());
				menFeedbackService.update(menFeedback);
			}
		}
		return toAjax(ids.length);
		//return toAjax(menFeedbackService.deleteMenFeedbackByIds(ids));
	}
}
